package sqlx

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

var db *sqlx.DB

type User struct {
	ID      int64  `json:"id"`
	Name    string `json:"name"`
	Phone   string `json:"phone"`
	Address string `json:"address"`
}

func configSqlx() {
	var err error
	const username = "test"
	const password = "Test@123"
	const databaseName = "test_db"

	dsn := fmt.Sprintf("%v:%v@tcp(127.0.0.1:3306)/%v?charset=utf8mb4&parseTime=True&loc=Local",
		username, password, databaseName)

	// 使用 MustConnect 连接的话,验证失败不成功直接panic
	//db := sqlx.MustConnect("mysql", dsn)

	//使用 Connect 连接,会验证是否连接成功,
	db, err = sqlx.Connect("mysql", dsn)

	if err != nil {
		fmt.Printf("connect DB failed, err:%v\n", err)
		return
	}
	//db.SetMaxOpenConns(20)
	//db.SetMaxIdleConns(10)
}

